package interfaze;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import constants.ResolutionOptions;

import logic.EarthDefenderConfig;
import logic.NoSaveException;
import logic.UIConfig;
import logic.Utilities;

@SuppressWarnings("serial")
public class SpaceInvadersOptionsPanel extends JPanel implements ActionListener, MouseListener, ItemListener, ChangeListener{
	
	SuperArcade2in1 window;
	private JLabel lResolution = new JLabel("Resolution:");						//The resolution's label. 
	private JLabel lFullScreen = new JLabel("Set Full Screen:");				//The fullscreen's label.
	private JLabel lDecoration = new JLabel("Set Decoration:");					//The decoration's label.
	private JCheckBox cbFullScreen = new JCheckBox();							//Full Screen Check Box.
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private JComboBox cbResolution = new JComboBox(ResolutionOptions.getValues());		//Resolution ComboBox.
	private JCheckBox cbDecoration = new JCheckBox();							//The Decoration Check Box.
	private JLabel lLifesNum = new JLabel("Lifes Number:");						//The lives number label.
	private JLabel lCurrentLives = new JLabel();
	private JLabel lEnemyShotP = new JLabel("Enemy shot probability:");			//The probability label.
	private JLabel lCurrentShotP = new JLabel();
	private JLabel lEnemyShotV = new JLabel("Enemy shot velocity:");			//The velocity label.
	private JLabel lCurrentShotV = new JLabel();
	private JLabel lBouncers = new JLabel("Bouncers");							//Bouncers label.
	private JSlider slLives = new JSlider();									//The lives's slider.
	private JSlider slShotP = new JSlider();									//The probability's slider.
	private JSlider slShotV = new JSlider();									//The velotity's slider.
	private JCheckBox cbBouncers = new JCheckBox();
	JLabel background = new JLabel();
	JButton bBack = new JButton("Back");
	Font font;
	
	public SpaceInvadersOptionsPanel(SuperArcade2in1 window) {
		this.window = window;
		this.setLayout(null);
		
		font = new Font("Helvetica", Font.PLAIN, Math.round(12 * window.xScale));
		
		Image image = Utilities.loadImage("res/EarthDefenderBackground.png");
		if(window.xScale !=1 && window.yScale != 1) {
			image = Utilities.resizeImage(window.xScale, window.yScale, "res/EarthDefenderBackground.png");
		}
		
		background.setIcon(new ImageIcon(image));
		background.setBounds(0, 0, window.width, window.height);
		
		bBack.setFont(font);
		bBack.setBounds(Math.round(75 * window.xScale), Math.round(590 * window.yScale), Math.round(100 * window.xScale),
				Math.round(25 * window.yScale));
		
		lResolution.setFont(font);
		lResolution.setForeground(Color.WHITE);
		lResolution.setBounds(Math.round(75 * window.xScale), Math.round(350 * window.yScale),
				Math.round(75 * window.xScale), Math.round(25 * window.yScale));
		
		cbResolution.setFont(font);
		cbResolution.setBounds(Math.round(150 * window.xScale), Math.round(350 * window.yScale), 
				Math.round(136 * window.xScale), Math.round(25 * window.yScale));
		cbResolution.setSelectedItem(logic.Utilities.resolutionToStringParser(window.width, window.height));
		
		lFullScreen.setFont(font);
		lFullScreen.setForeground(Color.WHITE);
		lFullScreen.setBounds(Math.round(75 * window.xScale), Math.round(380 * window.yScale),
				Math.round(95 * window.xScale), Math.round(25 * window.yScale));
		
		cbFullScreen.setBounds(Math.round(165 * window.xScale), Math.round(380 * window.yScale),
				Math.round(32 * window.xScale), Math.round(25 * window.yScale));
		cbFullScreen.setOpaque(false);
		
		lDecoration.setFont(font);
		lDecoration.setForeground(Color.WHITE);
		lDecoration.setBounds(Math.round(77 * window.xScale), Math.round(410 * window.yScale),
				Math.round(92 * window.xScale), Math.round(25 * window.yScale));
		
		cbDecoration.setBounds(Math.round(165 * window.xScale), Math.round(410 * window.yScale),
				Math.round(32 * window.xScale), Math.round(25 * window.yScale));
		cbDecoration.setOpaque(false);
		
		if(!window.isUndecorated()) {
			cbDecoration.setSelected(true);
		}
		if(window.isOnFullScreen) {
			cbDecoration.setEnabled(false);
			lResolution.setVisible(false);
			cbResolution.setVisible(false);
			cbFullScreen.setSelected(true);
		}
		
		lLifesNum.setFont(font);
		lLifesNum.setForeground(Color.WHITE);
		lLifesNum.setBounds(Math.round(300 * window.xScale), Math.round(350 * window.yScale), 
				Math.round(85 * window.xScale), Math.round(25 * window.yScale));
		slLives.setOpaque(false);
		slLives.setMinimum(1);
		slLives.setMaximum(5);
		slLives.setBounds(Math.round(385 * window.xScale), Math.round(350 * window.yScale), 
				Math.round(136 * window.xScale), Math.round(25 * window.yScale));
		lCurrentLives.setFont(font);
		lCurrentLives.setForeground(Color.WHITE);
		lCurrentLives.setBounds(Math.round(521 * window.xScale), Math.round(350 * window.yScale), 
				Math.round(136 * window.xScale), Math.round(25 * window.yScale));
		lEnemyShotP.setFont(font);
		lEnemyShotP.setForeground(Color.WHITE);
		lEnemyShotP.setBounds(Math.round(300 * window.xScale), Math.round(380 * window.yScale), 
				Math.round(137 * window.xScale), Math.round(25 * window.yScale));
		slShotP.setOpaque(false);
		slShotP.setMinimum(40);
		slShotP.setMaximum(100);
		slShotP.setBounds(Math.round(425 * window.xScale), Math.round(380 * window.yScale), 
				Math.round(136 * window.xScale), Math.round(25 * window.yScale));
		lCurrentShotP.setFont(font);
		lCurrentShotP.setForeground(Color.WHITE);
		lCurrentShotP.setBounds(Math.round(561 * window.xScale), Math.round(380 * window.yScale), 
				Math.round(136 * window.xScale), Math.round(25 * window.yScale));
		lEnemyShotV.setFont(font);
		lEnemyShotV.setForeground(Color.WHITE);
		lEnemyShotV.setBounds(Math.round(300 * window.xScale), Math.round(410 * window.yScale), 
				Math.round(136 * window.xScale), Math.round(25 * window.yScale));
		slShotV.setOpaque(false);
		slShotV.setMinimum(50);
		slShotV.setMaximum(200);
		slShotV.setBounds(Math.round(436 * window.xScale), Math.round(410 * window.yScale), 
				Math.round(136 * window.xScale), Math.round(25 * window.yScale));
		lCurrentShotV.setFont(font);
		lCurrentShotV.setForeground(Color.WHITE);
		lCurrentShotV.setBounds(Math.round(572 * window.xScale), Math.round(410 * window.yScale), 
		Math.round(136 * window.xScale), Math.round(25 * window.yScale));
		
		lBouncers.setFont(font);
		lBouncers.setForeground(Color.WHITE);
		lBouncers.setBounds(Math.round(300 * window.xScale), Math.round(440 * window.yScale), 
				Math.round(100 * window.xScale), Math.round(25 * window.yScale));
		
		cbBouncers.setOpaque(false);
		cbBouncers.setBounds(Math.round(400 * window.xScale), Math.round(440 * window.yScale), 
				Math.round(136 * window.xScale), Math.round(25 * window.yScale));
		
		try {
			ArrayList<Object> config = EarthDefenderConfig.load(window.userName);
			slShotP.setValue((int) config.get(0));
			lCurrentShotP.setText("" + (int) config.get(0));
			slShotV.setValue((int) config.get(1));
			lCurrentShotV.setText("" + (int) config.get(1));
			slLives.setValue((int) config.get(2));
			lCurrentLives.setText("" + (int) config.get(2));
			cbBouncers.setSelected((boolean) config.get(3));
		} catch (NoSaveException e) {
			slShotP.setValue(40);
			lCurrentShotP.setText("" + 40);
			slShotV.setValue(100);
			lCurrentShotV.setText("" + 100);
			slLives.setValue(3);
			lCurrentLives.setText("" + 3);
			cbBouncers.setSelected(true);
		}
		
		this.add(lResolution);
		this.add(cbResolution);
		this.add(lFullScreen);
		this.add(cbFullScreen);
		this.add(lDecoration);
		this.add(cbDecoration);
		this.add(lLifesNum);
		this.add(slLives);
		this.add(lCurrentLives);
		this.add(lEnemyShotP);
		this.add(slShotP);
		this.add(lCurrentShotP);
		this.add(lEnemyShotV);
		this.add(slShotV);
		this.add(lCurrentShotV);
		this.add(lBouncers);
		this.add(cbBouncers);
		this.add(bBack);
		this.add(background);
		
		cbResolution.addActionListener(this);
		cbDecoration.addItemListener(this);
		cbFullScreen.addItemListener(this);
		slLives.addChangeListener(this);
		slShotP.addChangeListener(this);
		slShotV.addChangeListener(this);
		cbBouncers.addItemListener(this);
		bBack.addActionListener(this);
		bBack.addMouseListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(bBack)) {
			Boolean bool = cbDecoration.isSelected();
			String resolution = cbResolution.getSelectedItem().toString();
			if(window.isOnFullScreen) {
				bool = new Boolean(false);
				resolution = "1280x720";
			}
			EarthDefenderConfig.write(window.userName, slShotP.getValue(), slShotV.getValue(), slLives.getValue(), cbBouncers.isSelected());
			UIConfig.write(window.userName, cbFullScreen.isSelected(), resolution, bool);
			//Redirects to the main menu.
			window.add(new SpaceInvadersMainMenuPanel(window));
			this.setVisible(false);
			window.remove(this);
		}	else if(e.getSource().equals(cbResolution)) {
				//Changes the resolution of the JFrame and the panel.
				Utilities.stringToResolutionParser((String) cbResolution.getSelectedItem(), window);
				if(window.width >= (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth())) {
						window.setSize(window.width, window.height);
						window.setLocation(0, 0);
				} else {
					window.setBounds((window.FULLSCREEN_WIDTH / 2) - (window.width / 2), 
							(window.FULLSCREEN_HEIGHT / 2) - (window.height / 2), window.width, window.height);
				}
				window.add(new SpaceInvadersOptionsPanel(window));
				this.setVisible(false);
				window.remove(this);
			}
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		if(e.getSource() == cbDecoration) {
			window.dispose();
			//Sets the decoration of the JFrame.
			if(cbDecoration.isSelected()) {
				window.setUndecorated(false);
			} else {
				window.setUndecorated(true);
			}
			window.setVisible(true);
		} else if(e.getSource().equals(cbFullScreen)) {
			if(cbFullScreen.isSelected()) {
				Utilities.windowToFullScreen(window);
				window.add(new SpaceInvadersOptionsPanel(window));
				this.setVisible(false);
				window.remove(this);
			} else {
				Utilities.stringToResolutionParser("1280x720", window);
				if(window.width >= (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth())) {
					window.setSize(window.width, window.height);
					window.setLocation(0, 0);
				} else {
					window.setBounds((window.FULLSCREEN_WIDTH / 2) - (window.width / 2), 
							(window.FULLSCREEN_HEIGHT / 2) - (window.height / 2), window.width, window.height);
				}
				window.add(new SpaceInvadersOptionsPanel(window));
				this.setVisible(false);
				window.remove(this);
			}
		} else if(e.getSource().equals(cbBouncers)) {
			
		}
	}
	
	@Override
	public void stateChanged(ChangeEvent e) {
		if(e.getSource().equals(slLives)) {
			lCurrentLives.setText("" + slLives.getValue());
		} else if(e.getSource().equals(slShotP)) {
			lCurrentShotP.setText("" + slShotP.getValue());
		} else if(e.getSource().equals(slShotV)) {
			lCurrentShotV.setText("" + slShotV.getValue());
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		if(e.getSource().equals(bBack)) {
			logic.Utilities.buttonEffect(bBack, 5, window.xScale, window.yScale);
		}
	}

	@Override
	public void mouseExited(MouseEvent e) {
		if(e.getSource().equals(bBack)) {
			logic.Utilities.buttonEffect(bBack, -5, window.xScale, window.yScale);
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		
	}
}